package com.zhiyizhen.callnumber.component;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.zhiyizhen.callnumber.R;
import com.zhiyizhen.callnumber.common.Base64ImageLoader;
import com.zhiyizhen.callnumber.common.ClickUtil;
import com.zhiyizhen.callnumber.common.DataCache;
import com.zhiyizhen.callnumber.common.DateUtil;
import com.zhiyizhen.callnumber.common.LogUtil;
import com.zhiyizhen.callnumber.common.MD5Util;
import com.zhiyizhen.callnumber.common.RxJavaUtil;
import com.zhiyizhen.callnumber.common.sp.SpConstant;
import com.zhiyizhen.callnumber.common.sp.SpUtil;
import com.zhiyizhen.callnumber.common.ToastUtil;
import com.zhiyizhen.callnumber.common.view.BaseActivity;
import com.zhiyizhen.callnumber.component.api.IApiService;
import com.zhiyizhen.callnumber.component.bean.CaptchaImageBean;
import com.zhiyizhen.callnumber.component.bean.LoginBean;
import com.zhiyizhen.callnumber.component.bean.LoginReq;
import com.zhiyizhen.callnumber.databinding.ActivityLoginBinding;
import com.zhiyizhen.callnumber.net.ResponseException;
import com.zhiyizhen.callnumber.net.ServiceFactory;
import com.zhiyizhen.callnumber.net.response.ResponseData;
import com.zhiyizhen.callnumber.net.response.ResponseObserver;

public class LoginActivity extends BaseActivity {
    private ActivityLoginBinding binding;
    private String uuid;
    private String image;

    @SuppressLint("SetTextI18n")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.loginBtn.setOnClickListener(v -> {
            if (!ClickUtil.isValid()) {
                return;
            }
            login();
        });
        binding.dateTv.setText(DateUtil.getCurrentWeekday() + " " + DateUtil.getCurrentDate());
        binding.timeTv.setText(DateUtil.getCurrentTime());
        binding.captchaImage.setOnClickListener(v -> requestCaptchaImage());
        requestCaptchaImage();
    }

    private void login() {
        String userName = binding.userNameTv.getText().toString();
        String password = binding.passwordTv.getText().toString();
        String code = binding.captchaImageResult.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showShort(R.string.pls_input_admin);
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.showShort(R.string.pls_input_password);
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showShort(R.string.pls_input_captcha);
            return;
        }
        LoginReq req = new LoginReq(userName, MD5Util.md5MessageDigest(password));
        req.setUuid(uuid);
        req.setCode(code);

        ServiceFactory.getService(IApiService.class)
                .login(req)
                .compose(RxJavaUtil.mainSchedulers())
                .as(this.bindLifecycle())
                .subscribe(new ResponseObserver<>() {

                    @Override
                    protected void onSuccess(ResponseData<LoginBean> responseData) {
                        DataCache.setLoginBean(responseData.getData());
                        LogUtil.d("LoginBean" + responseData.getData().getClinicId());
                        SpUtil.putInt(SpConstant.CLINIC_ID, responseData.getData().getClinicId());
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }
                    @Override
                    protected void onFailure(ResponseException e) {
                        super.onFailure(e);
                        requestCaptchaImage();
                    }
                });
    }

    private void requestCaptchaImage() {
        ServiceFactory.getService(IApiService.class)
                .requestCaptchaImage()
                .compose(RxJavaUtil.mainSchedulers())
                .as(this.bindLifecycle())
                .subscribe(new ResponseObserver<>() {

                    @Override
                    protected void onSuccess(ResponseData<CaptchaImageBean> responseData) {
                        if (responseData != null && responseData.getData() != null) {
                            LogUtil.d("responseData---" + responseData.getData().getUuid());
                            uuid = responseData.getData().getUuid();
                            image = responseData.getData().getImg();
                            Base64ImageLoader.loadBase64Image(image, binding.captchaImage);
                        }
                    }
                });
    }
}
